package com.naihe.anderson.sms.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.entity.CapitalInfo;
import com.naihe.anderson.sms.manager.RecordMoneyMgr;

public class CapitalInfoAdapter extends BaseAdapter{

    Context mContext;
    List<CapitalInfo> objects;
    RecordMoneyMgr moneyMgr;
    
    public CapitalInfoAdapter(Context context,List<CapitalInfo> objects) {
        mContext = context;
        moneyMgr = ((NhSmsApp)context.getApplicationContext()).getRecordMoneyMgr();
        this.objects = objects;
    }

    @Override
	public int getCount() {
		if(objects == null) return 0;
		return objects.size();
	}

	@Override
	public CapitalInfo getItem(int position) {
		if(objects == null) return null;
		return objects.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View view = null;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            view = inflater.inflate(R.layout.capital_info_item, null);
        } else {
            view = convertView;
        }
        
        CapitalInfo entityObj = objects.get(position);
        
        final LinearLayout detial_layout = (LinearLayout) view.findViewById(R.id.detial_layout);
        ImageButton go_detial = (ImageButton)view.findViewById(R.id.go_detial);
        
        go_detial.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(!detial_layout.isShown()){
		        	detial_layout.setVisibility(View.VISIBLE);
		        	v.setBackgroundResource(R.drawable.main_fc_arrow_btn);
		        } else {
		        	v.setBackgroundResource(R.drawable.main_fc_arrow_point_down_btn);
		        	detial_layout.setVisibility(View.GONE);
		        }
			}
		});
        
        TextView phone_number = (TextView) view.findViewById(R.id.phone_number);
        TextView name = (TextView) view.findViewById(R.id.name);
        name.setText(moneyMgr.getPhoneBank(entityObj.number));
        
        phone_number.setText(entityObj.number);
        
        TextView income_keys_et = (TextView) view.findViewById(R.id.income_keys_et);
        income_keys_et.setText(entityObj.incomeIeys);
		TextView expenditure_keys_et = (TextView) view.findViewById(R.id.expenditure_keys_et);
		expenditure_keys_et.setText(entityObj.expenditureKeys);
        return view;
    }
    
}
